package com.kingtone.rongji;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.baidu.mapapi.map.MyLocationData;
import com.kingtone.rongji.adapter.StoreAdapter;
import com.kingtone.rongji.biz.Address;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.LocationService;

public class StoreListActivity extends BaseActivity {

	private TextView title = null;
	private ImageView backBtn = null;
	private ImageView leftIcon = null;
	private TextView rightTitle = null;
	private Button seletFromMapBtn = null;
	private Context mContext = StoreListActivity.this;
	private ListView storeList = null;
	private ArrayList<Address> list = null;
	private Address defaultAddress = null;
	private double currentLat, currentLng;
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.activity_store_list);
		Intent intent = getIntent();
		if(intent.hasExtra("storeList"))
		{
			list = (ArrayList<Address>)intent.getSerializableExtra("storeList");
		}
		if(intent.hasExtra("defaultAdd"))
		{
			defaultAddress = (Address)intent.getSerializableExtra("defaultAdd");
		}
		
		if (LocationService.locData != null)
	    {
		  MyLocationData mLocation = LocationService.locData;
	      currentLat = mLocation.latitude;
	      currentLng = mLocation.longitude;
	    }
		setupView();
		
	}
	
	private void setupView()
	{
		title = (TextView)findViewById(R.id.title);
		title.setText(getString(R.string.store_select_title));
		rightTitle =(TextView)findViewById(R.id.right_title);
		rightTitle.setText(getString(R.string.store_select_title));
		
		leftIcon = (ImageView)findViewById(R.id.left_icon);
		leftIcon.setBackgroundResource(R.drawable.icon_zhi);
		
		seletFromMapBtn = (Button)findViewById(R.id.store_from_map);
		seletFromMapBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				Intent intent = new Intent(StoreListActivity.this, StoreMapListActivity.class);
				intent.putExtra("storeList", list);
				startActivityForResult(intent, 101);
			}
		});
		
		storeList = (ListView)findViewById(R.id.store_list);
		if(list != null && list.size() > 0)
		{
			StoreAdapter storeAdapter = new StoreAdapter(mContext, list, currentLat, currentLng, defaultAddress);
			storeList.setAdapter(storeAdapter);
		}
		//点击设置为默认地址
		storeList.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				if(list != null)
				{
					Address add = list.get(position);
					Intent intent = new Intent();
					intent.putExtra(AppContasts.STORE_NAME, add.getShipTo());
					intent.putExtra(AppContasts.STORE_ADDRESS, add.getAddress());
					setResult(RESULT_OK,intent);
					finish();
				}
			}
		});
		
		backBtn = (ImageView)findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				onBackPressed();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == 101)
		{
			if (resultCode == RESULT_OK)
			{
				if(data != null)
				{
					String storeName = null;
					String storeAdd = null;
					
					if(data.hasExtra(AppContasts.STORE_NAME))
					{
						storeName = data.getStringExtra(AppContasts.STORE_NAME);
					}
					if(data.hasExtra(AppContasts.STORE_NAME))
					{
						storeAdd = data.getStringExtra(AppContasts.STORE_ADDRESS);
					}
					Intent intent = new Intent();
					intent.putExtra(AppContasts.STORE_NAME, storeName);
					intent.putExtra(AppContasts.STORE_ADDRESS, storeAdd);
					setResult(RESULT_OK,intent);
					finish();
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
}
